<?php

/**

 *

 */

class PublicAction extends BaseAction
{	 
	 function verify(){
		  
		import('ORG.Util.Image');
		
        Image::buildImageVerify();
		
      }

	function send(){
		$code = rand(1111,9999);

		session("code",$code);
		$phone = I('post.phone');
		$phoneNum = M('User')->where(array('phone'=>$phone))->count();
		if($phoneNum >0){
			$this->ajaxReturn(array('status'=>0,'msg'=>'手机号码已经存在 不能重复注册'));

			exit();
		}
		$tools = new ToolsModel();
//		$info ="您正在进行实名认证操作,您的验证码是:".$code." 如非本人操作可以忽略";
//		$tools->sendmsg($phone,$info);
		$data = $tools->sendmsg($phone,'', array($code));
		$data = json_decode($data, true);
		$msg 	= $data['result'] == 0 ? '验证码已经发送到您的手机' : $data['errmsg'];
		$state	= $data['result'] == 0 ? 1 : "2";
		$this->ajaxReturn(array('status'=>$state,'msg'=>$msg));
	}

    function t(){


	}

	 private $asklist =array(

	   array("ask"=>"请问⑩加上⑥等于多少","q"=>"16"),

	    array("ask"=>"问⑥乘于①等于多少","q"=>"6"),

	     array("ask"=>"请问⑥加上⑦等于多少","q"=>"13"),

	      array("ask"=>"请问①加上⑩等于多少","q"=>"11"),

	       array("ask"=>"请问⑩加上⑧等于多少","q"=>"18"),

	        array("ask"=>"请问⑧乘于④等于多少","q"=>"32"),

	         array("ask"=>"请问③加上⑤等于多少","q"=>"8"),

	          array("ask"=>"请问④乘于④等于多少","q"=>"16"),

	           array("ask"=>"请问②乘于⑥等于多少","q"=>"12"),

	            array("ask"=>"请问④加上⑤等于多少","q"=>"9"),

	);
	  
    function jubao(){
		  
        $id=isset($_GET['id'])?$_GET['id']:$this_>error("信息错误");

		$li =  M('Zhaopin')->where('id='.$id)->find();
		
		if(isset($_POST['submit'])){
			
				
				
				$data['contact']=$_POST['contact'];
				
				$data['title']=$li['title'];
				
				$data['content']=$_POST['content'];
				
				$data['type']=$_POST['type'];
				
				M('accusation')->add($data);
				
				header("Location: ".U('Public/jubao'));  exit(); 
			}else{
	
		
		$this->assign("li",$li);
		
		$this->display();
			}
	}	
	 
	function delinfo(){	
	
		$id=isset($_GET['id'])?$_GET['id']:$this_>error("信息错误");
		
			$data =  M('Zhaopin')->where('id='.$id)->find();
			
		if(isset($_POST['submit'])){
			
		if( ($data['delpass'] == $_POST['tb_delpassword']) && ($data['id'] == $id) ){
		
		M('Zhaopin')->where('id='.$id)->delete();
		
		 	header("Location: ".U('Public/bianji'));  exit(); 
	
		}
		
		/*
			

					$Zhaopin =   M('Zhaopin');
					$Zhaopin->where('id='.$id)->delete();
						
				}else{
					
					

					
					$this->assign("data",$data);
					
  			        $this->display();	
	
				}
				*/
								
	   }
	   
	   
	
		$catepath = M('category')->where("id=".$data['cid'])->getField("path");
		
		$citypy = M('city')->where("id=".$data['cityid'])->getField("pinyin");
		
		$this->assign("data",$data);


		
			 
  		$this->display();
	}	

    function xiugai(){
				
		$id = empty($_GET['id'])?exit():$_GET['id'];
				
		$data =  M('Zhaopin')->where('id='.$id)->find();
		
		$catepath = M('Category')->where('id='.$data['cid'])->getField("path");
		
		$this->assign("info",$data);
					
		$uid = $data['uid'];  /*招聘表中的到 用户id*/
		
		$comid = $data['company_id']; /*招聘表中的到 公司id*/
		
		$cateid = $data['cid'];
		
		$comdata = M('Company')->where('id='.$comid)->find(); 
		 
		$catedata = M('Category')->where('id='.$cateid)->find(); 

		$user = M('User')->where('uid='.$uid)->find();		
		
		$sheng=M('city')->where(' status=0 AND  cid=0')->select();
		
		$cityid = $data['cityid'];//当前信息所属的城市ID
		
		$citypy = M('city')->where("id=".$cityid)->getField('pinyin');
		
		$cid = M('city')->where("id=".$cityid)->getField('cid');
		
		$this->assign("cid",$cid);//当前的省的ID
		
		$city  = M('city')->where("cid=".$cid)->select();
		
		$this->assign("sheng",$sheng);
		
		$this->assign("city",$city);

		foreach ($temp as $v){
			
			$v['city'] = M('City')->where("cid=".$v['id'])->select();
			
			$city[]=$v;
			
		}
		

			/*判断是否为当前用户和用户登录*/
			
			if($this->uinfo ==0){
				
		 		header("Location: ".U('Public/login'));exit();
				
	        }else if($this->uinfo['username'] != $user['username']){
				
				header("Location: ".U('Public/bianji'));	exit();
			}else {
				
				  }		
				
					if(isset($_POST['submit'])){
				      
				     $Zhaopin = M("Zhaopin");
				
				      $Zhaopin->title=$_POST['title'];
					  
					  $Zhaopin->content=$_POST['content'];
					  
					  $Zhaopin->keywords=$_POST['keywords'];
					
					  $Zhaopin->shopname=$_POST['shopname'];

 	  	  	 		  $Zhaopin->contact=$_POST['contact'];

 	  	  	          $Zhaopin->phone=$_POST['phone'];

// 	  	  	          $Zhaopin->email=$_POST['email'];
					  
					  //$Zhaopin->cover=$this->upfile();
                        if ($_POST['photo_base64'] !==""){
                            $Zhaopin->cover=$this->saveBase64Image($_POST['photo_base64']);
                        }
					   
					  $Zhaopin->delpass=$_POST['delpass'];
					  
					  $Zhaopin->qq=$_POST['qq'];
					  
					  $Zhaopin->status=0;
					  
					  $Zhaopin->swt=$_POST['swt'];
					  
					  $Zhaopin->cityid=$_POST['cityid'];
					  
					  $Zhaopin->cid=$data['cid'];
						$Zhaopin->weixin = $_POST['weixin'];
						$Zhaopin->perform = $_POST['perform'];
					  
					  if($Zhaopin->where("id=".$id)->save()){
					  		 header("Location: ".U('User/infolist'));
				 	  	  	 
					  }else{
				 	  	  	header("Location: ".U('Public/xiugai'));
				 	  	  	 	
				 	   }
				     			
					}else{
						
							$this->assign('list',$city);
							$this->assign("data",$data);
							$this->assign("comdata",$comdata);
							$this->assign("catedata",$catedata);
							$this->assign("user",$user);
							
							/*提问*/			
							$rand = rand(0,9);
							$ask = $this->asklist[$rand];
							$ask['index']=$rand;
							$this->assign("ask",$ask);
					
							$this->display();	
						
						
						}
	 }
	 
	 
	  
    function getcity(){
		   $id = $_GET['id'];
		   $list = M('city')->where("cid=".$id)->select();
		    echo(json_encode($list));
		 
		 } 
	
	function bianji(){
				
		  $this->display();
		
		
		}


		function getarea(){
		    $id = I('get.id');
		   $temp =  M('Area')->where(array('cityid'=>$id))->select();
		   if(empty($temp)){
		   	$this->ajaxReturn(array('status'=>0,'msg'=>'没有数据'));
		   }else{
		   	   foreach ($temp as $v){
		   	   	  
		   	   	 $list[]=$v;
			   }
               $this->ajaxReturn(array('status'=>1,'result'=>$list));
		   }
		}
	  

	/**

	 * 会员登录

	 * Enter description here ...

	 */

	public function login(){

	    if(isset($_POST['submit'])){

	      $info = M('User')->where("username='".$_POST['username']."'")->find();	

	     

	      if($info['password'] == md5($_POST['password'])){
			
	      	  writeUserCookie($info['uid'], $info['username'], $info['password']);
	
	      	  M('User')->where('uid='.$info['uid'])->setField('createtime',mktime());
	      	  
	      	 // $this->success("登录成功",U('User/index'));

	      	 header("Location: ".U('Public/denglu')); 

	      }else{

	      	

	      	header("Location: ".U('Public/login'));

	      }	

	    }else{



		$this->display();

	    }

	}

	

	/**

	 * 用户注册

	 * Enter description here ...

	 */

	function reg(){

      

		if(isset($_POST['submit'])){

			    if($_SESSION['code'] != $_POST['code']){
                 $this->error('手机验证码错误');


				}


		 	  	if($_SESSION['verify'] != md5($_POST['verify'])){

 	  	  	  	   $this->error('验证码错误');

 	  	  	   }
              $emailNum = M('User')->where(array('email'=>$_POST['email']))->count();
		 	  	if($emailNum >0){

                    $this->error('这个邮箱系统已经存在 请更换其他邮箱试试');
				}

 	  	  	   $count = M('User')->where("username='".$_POST['username']."'")->count();

 	  	  	   

 	  	  	   if($count > 0){

 	  	  	   	$this->error("抱歉用户名已经存在！");

 	  	  	   	exit();

 	  	  	   }

			$data["cityid"] = $_POST["city"];

			$data["username"] = $_POST["username"];

			$data["password"] = md5($_POST["userpassword"]);

			$data["email"] = $_POST["email"];
			$data['phone'] = I('post.phone');

			$data["createtime"] = mktime();

			if($uid = M('User')->add($data)){

                writeUserCookie($uid, $data['username'], $data['password']);

                     //写入企业表

                	 //写入企业表

                	if($_POST['category']==1){

                		//dump($_POST);exit();

                		 $c['cityid']= $_POST['city'];

                		 $c['uid']=$uid;

                		 $c['category']=$_POST['category'];

                		 $c['product']=$_POST['product'];
                		 
                		 $c['contact']=$_POST['contact'];

                		 $c['name']=$_POST['name'];

                		 $c['address']=$_POST['address'];

                		 $c['phone']=$_POST['tel'];

                		 $c['email']=$_POST['email'];

                		 $c['status']=0;

                		 $c['createtime']=mktime();

             

				         M('Company')->add($c);

                	}else{

                		

                		//涓汉

                		 $c['cityid']= $_POST['city'];

                		 $c['uid']=$uid;

                		 $c['category']=$_POST['category'];

                		 $c['product']=$_POST['product'];
                		 
						$c['contact']=$_POST['contact'];
						
                		 $c['name']=$_POST['username'];

                		 $c['address']=$_POST['address'];

                		 $c['phone']=$_POST['tel'];

                		 $c['email']=$_POST['email'];

                		 $c['status']=0;

                		 $c['createtime']=mktime();

             

				         M('Company')->add($c);

                	}

				//$this->success("注册成功",U('User/index'));
				 header("Location: ".U('User/index')); 

			}

			else {

				$this->error("注册失败");

			}

		}else{

				

            $this->list =M('City')->where("status=0 AND city != '' ")->order("initial ASC")->select();

               $rand = rand(0,9);

               $ask = $this->asklist[$rand];

               $ask['index']=$rand;

 	  	  	   $this->assign("ask",$ask);

 	  	  	 //  dump($ask);

 	  	  	  

			$this->display();

		}


	}

/*检查删除密码*/

	function checkDeletcode(){
		 $count = M('Zhaopin')->where("username='".$_GET['tb_delpassword']."'"& "username='".$_GET['tb_delpassword']."'")->count();
		  if($count > 0 ){
		  	 echo 1;
		  }else{
		  	echo 0;
		  }
	}
	
	

	
/*检查问题*/
	function checkAsk(){

		$index = $_GET['index'];

		

		$s = $_GET['yzask'];

	   $info = $this->asklist[$index];



	   if($info['q']== $s){

	   	   echo(1);

	   }else{

	   	echo(0);

	   	

	   }

		

		

	}

	

	
	/*验证图片*/
	function checkVerify(){

		$verify = $_GET['verify'];

		if(md5($verify) ==$_SESSION['verify'] ){

			 echo 1;

		}else{

			

			echo 0;

		}

		 

		

	}

	

	/*

	 * 检查用户名

	 * Enter description here ...

	 */
	function checkUsername(){
		 $count = M('User')->where("username='".$_GET['username']."'")->count();
		  if($count > 0 ){
		  	 echo 1;
		  }else{
		  	echo 0;

		  }
	}

	

	/*

	 * 检查邮箱

	 * Enter description here ...

	 */

	function checkEmail(){

	 $count = M('User')->where("email='".$_GET['email']."'")->count();

		  if($count > 0 ){

		  	 echo 1;

		  }else{
		  	echo 0;
		  }
	}

	

	

	function logout(){
		logout();
		header("Location: ".U('Index/index')); 
		//$this->success("退出登录成功！",U('Index/index'));
	}

	
	
   function suggest(){
	if(isset($_POST['submit'])){
		
		$Suggest = M('suggest');
		
		$data['title'] = $_POST['title'];
		
		$data['content'] = $_POST['content'];
		
		$data['contact'] = $_POST['contact'];
		
		 $Suggest->add($data);
        	
			$this->success("您的意见已经提交");
			
    
		
	}else{
			$this->display();
		}
	}
	

	

	public function forgotpass(){
		     
			if(isset($_POST['submit'])){
				$username = $_POST['username'];
				$useremail = $_POST['useremail'];
				 $info = M('User')->where("username='".$username."'")->find();
				 if($info['email']==$useremail){
				 	$newpass = mktime();
				 	 M('User')->where("username='".$username."'")->setField("password", md5($newpass));
				   vendor("Email.smtp");
					$smtpserver = "smtp.126.com";//SMTP服务器
					$smtpserverport =25;//SMTP服务器端口
					$smtpusermail = "yeczxw@126.com";//SMTP服务器的用户邮箱
					$smtpemailto = $useremail;//发送给谁
					$smtpuser = "yeczxw@126.com";//SMTP服务器的用户帐号
					$smtppass = "omealqfsbjybyfnn";//SMTP服务器的用户密码
					$mailtitle = "夜场招聘网找回密码";//邮件主题
					$mailcontent = "您的新密码是：".$newpass;//邮件内容
					$mailtype = "HTML";//邮件格式（HTML/TXT）,TXT为文本邮件
					//************************ 配置信息 ****************************
					$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
					$smtp->debug = false;//是否显示发送的调试信息
					$state = $smtp->sendmail($smtpemailto, $smtpusermail, $mailtitle, $mailcontent, $mailtype);
				    if($state){
				    	
				    	$this->success("新密码已经发到您的邮箱");
				    }else{
				    	  $this->error("发送邮件失败");
				    	
				    }
				 }else{
				 	
				 	$this->error("用户名和邮箱不匹配");
				 }
					
			}else{
			  $this->display();
			}
      }

      
      
      /**
       * 上传图片
       * Enter description here ...
       */
      function upfile(){
     
      	import('ORG.Net.UploadFile');
      	$path=date("Ym").'/';
      	$upload = new UploadFile();// 实例化上传类
      	$upload->maxSize  = 3145728 ;// 设置附件上传大小
      	$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
      	$upload->savePath =  './Data/photo/'.$path;// 设置附件上传目录
      	if(!file_exists("./Data/photo/".$path)){
      		mkdir("./Data/photo/".$path);
      	}
      	if(!$upload->upload()) {
      		// 上传错误提示错误信息
       
             return $upload->getErrorMsg();
      	}else{

      		$info = $upload->getUploadFileInfo(); //获取上传结果信息
      		$photopath= str_replace(".","",$info[0]['savepath']);
      		$s=$photopath.$info[0]['savename'];
      		    import('ORG.Util.Image');
			$Image = new Image();
			// 给avator.jpg 图片添加logo水印
			$Image->water('.'.$s,'avator.png','',80);
      		return $s;
     


      	}



      	 
      }


      function  upphoto(){
          import('ORG.Net.UploadFile');
          $path=date("Ym").'/';
          $upload = new UploadFile();// 实例化上传类
          $upload->maxSize  = 3145728 ;// 设置附件上传大小
          $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
          $upload->savePath =  './Data/photo/'.$path;// 设置附件上传目录
          if(!file_exists("./Data/photo/".$path)){
              mkdir("./Data/photo/".$path);
          }
          if(!$upload->upload()) {
              // 上传错误提示错误信息


              $this->ajaxReturn(array('status'=>0,'msg'=>$upload->getErrorMsg()));
          }else{

              $info = $upload->getUploadFileInfo(); //获取上传结果信息
              $photopath= str_replace(".","",$info[0]['savepath']);
              $s= $photopath.$info[0]['savename'];


              $this->ajaxReturn(array('status'=>1,'photo'=>$s));
          }
	  }


    /**
     * 保存64位编码图片
     */

    function saveBase64Image($base64_image_content){
        $image_url = "";
        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){

            //图片后缀
            $type = $result[2];

            //保存位置--图片名
            $image_name=date('His').str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT).".".$type;
            $image_url = '/Data/photo/'.date('Ym').'/'.$image_name;
            if(!is_dir(dirname('.'.$image_url))){
                mkdir(dirname('.'.$image_url));
                chmod(dirname('.'.$image_url), 0777);
                umask($oldumask);
            }

            //解码
            $decode=base64_decode(str_replace($result[1], '', $base64_image_content));
            if (file_put_contents(".".$image_url, $decode)){
                $data['code']=0;
                $data['imageName']=$image_name;
                $data['url']=$image_url;
                $data['msg']='保存成功！';
                return $image_url;
            }else{
                $data['code']=1;
                $data['imageName']='';
                $data['url']='';
                $data['msg']='图片保存失败！';
                return "";
            }
        }else{
            $data['code']=1;
            $data['imageName']='';
            $data['url']='';
            $data['msg']='base64图片格式有误！';
            return "";

        }

    }

	  function auto(){

      	file_put_contents("1.txt",date("Y-m-d H:i:s").PHP_EOL,FILE_APPEND);

          M('Zhaopin')->where(array('auto'=>1))->setField('createtime',time());
	  }
     
      
}

?>
